$(document).ready(function() {
	InitCarousel();
	InitPropertyCarousel();
	InitOffCanvasNavigation();
	InitMap();
	InitPriceSlider();
	InitSizeSlider();
	InitImageSlider();
	InitAccordion();
	InitTabs();
	InitPalette();
});

function InitPalette() {
	if ($.cookie("palette") == "true") {
		$(".palette").addClass("open")
	}
	$(".palette .toggle a").on( {
		click : function(f) {
			f.preventDefault();
			var d = $(this).closest(".palette");
			if (d.hasClass("open")) {
				d.animate( {
					left : "-195"
				}, 500, function() {
					d.removeClass("open")
				});
				$.cookie("palette", false)
			} else {
				d.animate( {
					left : "0"
				}, 500, function() {
					d.addClass("open");
					$.cookie("palette", true)
				})
			}
		}
	});
	if ($.cookie("color-variant")) {
		var c = $(".palette").find("a." + $.cookie("color-variant"));
		var b = c.attr("href");
		var a = c.attr("class");
		$("#color-variant").attr("href", b);
		$("body").addClass(a)
	}
	if ($.cookie("pattern")) {
		$("body").addClass($.cookie("pattern"))
	}
	if ($.cookie("header")) {
		$("body").addClass($.cookie("header"))
	}
	$(".palette a").on( {
		click : function(g) {
			g.preventDefault();
			if ($(this).closest("div").hasClass("colors")) {
				var f = $(this).attr("href");
				var d = $(this).attr("class");
				$("#color-variant").attr("href", f);
				if (!$("body").hasClass(d)) {
					$("body").removeClass($.cookie("color-variant"));
					$("body").addClass(d)
				}
				$.cookie("color-variant", d)
			} else {
				if ($(this).closest("div").hasClass("patterns")) {
					var d = $(this).attr("class");
					if (!$("body").hasClass(d)) {
						$("body").removeClass($.cookie("pattern"));
						$("body").addClass(d);
						$.cookie("pattern", d)
					}
				} else {
					if ($(this).closest("div").hasClass("headers")) {
						var d = $(this).attr("class");
						if (!$("body").hasClass(d)) {
							$("body").removeClass($.cookie("header"));
							$("body").addClass(d);
							$.cookie("header", d)
						}
					}
				}
			}
		}
	});
	$(".palette .reset").on( {
		click : function() {
			$("body").removeClass($.cookie("color-variant"));
			$("#color-variant").attr("href", null);
			$.removeCookie("color-variant");
			$("body").removeClass($.cookie("pattern"));
			$.removeCookie("pattern");
			$("body").removeClass($.cookie("header"));
			$.removeCookie("header")
		}
	})
}
function InitPropertyCarousel() {
	$(".carousel.property .content li img").on( {
		click : function(b) {
			var c = $(this).attr("src");
			var a = $(this).closest(".carousel.property").find(".preview img");
			a.attr("src", c);
			$(".carousel.property .content li").each(function() {
				$(this).removeClass("active")
			});
			$(this).closest("li").addClass("active")
		}
	})
}
function InitTabs() {
	$(".tabs a").click(function(a) {
		a.preventDefault();
		$(this).tab("show")
	})
}
function InitImageSlider() {
	$(".iosSlider").iosSlider(
			{
				desktopClickDrag : true,
				snapToChildren : true,
				infiniteSlider : true,
				navSlideSelector : ".slider .navigation li",
				onSlideComplete : function(a) {
					if (!a.slideChanged) {
						return false
					}
					$(a.sliderObject).find(".slider-info").attr("style", "");
					$(a.currentSlideObject).find(".slider-info").animate( {
						left : "15px",
						opacity : ".9"
					}, "easeOutQuint")
				},
				onSliderLoaded : function(a) {
					$(a.sliderObject).find(".slider-info").attr("style", "");
					$(a.currentSlideObject).find(".slider-info").animate( {
						left : "15px",
						opacity : ".9"
					}, "easeOutQuint")
				},
				onSlideChange : function(a) {
					$(".slider .navigation li").removeClass("active");
					$(
							".slider .navigation li:eq("
									+ (a.currentSlideNumber - 1) + ")")
							.addClass("active")
				},
				autoSlide : true,
				scrollbar : true,
				scrollbarContainer : ".sliderContainer .scrollbarContainer",
				scrollbarMargin : "0",
				scrollbarBorderRadius : "0",
				keyboardControls : true
			})
}
function InitAccordion() {
	$(".accordion").on(
			"show",
			function(a) {
				$(a.target).prev(".accordion-heading")
						.find(".accordion-toggle").addClass("active")
			});
	$(".accordion").on(
			"hide",
			function(a) {
				$(this).find(".accordion-toggle").not($(a.target)).removeClass(
						"active")
			})
}
function InitPriceSlider() {
        var rate = 1000000000;
	var a = jQuery(".property-filter .price-from input").val();
	var b = jQuery(".property-filter .price-to input").val();
        
        a = (a === "" ? 1 : a/rate);
	b = (b === "" ? 100 : b/rate);
	
	jQuery(".price-value .from").text('IDR ' + a + 'B');
//	jQuery(".price-value .from").currency( {
//		region : "IDR",
//		thousands : " ",
//		decimal : ",",
//		decimals : 0
//	});
	jQuery(".price-value .to").text('IDR ' + b + 'B');
//	jQuery(".price-value .to").currency( {
//		region : "IDR",
//		thousands : " ",
//		decimal : ",",
//		decimals : 0
//	});
	$(".property-filter .price-slider").slider(
			{
				range : true,
				min : 1,
				max : 100,
				values : [ a, b ],
				slide : function(c, d) {
					jQuery(".property-filter .price-from input").attr("value",
							d.values[0]*rate);
					jQuery(".property-filter .price-to input").attr("value",
							d.values[1]*rate);
					jQuery(".price-value .from").text('IDR ' + d.values[0] + 'B');
//					jQuery(".price-value .from").currency( {
//						region : "IDR",
//						thousands : " ",
//						decimal : ",",
//						decimals : 0
//					});
					jQuery(".price-value .to").text('IDR ' + d.values[1] + 'B');
//					jQuery(".price-value .to").currency( {
//						region : "IDR",
//						thousands : " ",
//						decimal : ",",
//						decimals : 0
//					});
				}
			});
}
function InitSizeSlider() {
	var a = jQuery(".property-filter .size-from input").val();
	var b = jQuery(".property-filter .size-to input").val();
	if (a == "") {
		a = 1
	}
	if (b == "") {
		b = 10000
	}
	jQuery(".size-value .from").text(a);
	jQuery(".size-value .to").text(b);
	$(".property-filter .size-slider").slider(
			{
				range : true,
				min : 1,
				max : 10000,
				values : [ a, b ],
				slide : function(c, d) {
					jQuery(".property-filter .size-from input").attr("value",
							d.values[0]);
					jQuery(".property-filter .size-to input").attr("value",
							d.values[1]);
					jQuery(".size-value .from").text(d.values[0]);
					jQuery(".size-value .to").text(d.values[1])
				}
			})
}
function InitEzmark() {
	$('input[type="checkbox"]').ezMark();
	$('input[type="radio"]').ezMark()
}
function InitChosen() {
	$("select").chosen( {
		disable_search_threshold : 10
	})
}
function InitOffCanvasNavigation() {
	$("#btn-nav").on( {
		click : function() {
			$("body").toggleClass("nav-open")
		}
	})
}
function InitCarousel() {
	$("#main .carousel .content ul").carouFredSel( {
		scroll : {
			items : 1
		},
		auto : false,
		next : {
			button : "#main .carousel-next",
			key : "right"
		},
		prev : {
			button : "#main .carousel-prev",
			key : "left"
		}
	});
	$(".carousel-wrapper .content ul").carouFredSel( {
		scroll : {
			items : 1
		},
		auto : false,
		next : {
			button : ".carousel-wrapper .carousel-next",
			key : "right"
		},
		prev : {
			button : ".carousel-wrapper .carousel-prev",
			key : "left"
		}
	})
}
function LoadMapProperty() {
	var e = $("#latListing").val();
	var c = $("#lngListing").val();
	var priceListing = $('#priceListing').val();
	var areaListing = $('#areaListing').val();
	var titleListing = $('#titleListing').val();
	var thumbListing = $('#thumbListing').val();
	var a = new Array( [ e, c ]);
	var f = new Array();
	var b = {
		center : new google.maps.LatLng(e, c),
		zoom : 14,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		scrollwheel : false
	};
	var d = new google.maps.Map(document.getElementById("property-map"), b);
	$
			.each(
					a,
					function(j, i) {
						var h = new google.maps.Marker(
								{
									position : new google.maps.LatLng(i[0],
											i[1]),
									map : d,
									icon : "http://html.realia.byaviators.com/assets/img/marker-transparent.png"
								});
						var g = {
							content : '<div class="infobox"><div class="image"><img src="'+ thumbListing +'" alt=""></div><div class="title"><a href="javascript:void();">'+ titleListing +'</a></div><div class="area"><span class="key">Area:</span><span class="value">'+ areaListing +'</span></div><div class="price">Price: '+ priceListing +'</div></div>',
							disableAutoPan : false,
							maxWidth : 0,
							pixelOffset : new google.maps.Size(-146, -190),
							zIndex : null,
							closeBoxURL : "",
							infoBoxClearance : new google.maps.Size(1, 1),
							position : new google.maps.LatLng(i[0], i[1]),
							isHidden : false,
							pane : "floatPane",
							enableEventPropagation : false
						};
						h.infobox = new InfoBox(g);
						h.infobox.isOpen = false;
						var g = {
							draggable : true,
							content : '<div class="marker"><div class="marker-inner"></div></div>',
							disableAutoPan : true,
							pixelOffset : new google.maps.Size(-21, -58),
							position : new google.maps.LatLng(i[0], i[1]),
							closeBoxURL : "",
							isHidden : false,
							enableEventPropagation : true
						};
						h.marker = new InfoBox(g);
						h.marker.open(d, h);
						f.push(h);
						google.maps.event.addListener(h, "click", function(l) {
							var k = this;
							$.each(f, function(n, m) {
								if (m !== k) {
									m.infobox.close();
									m.infobox.isOpen = false
								}
							});
							if (k.infobox.isOpen === false) {
								k.infobox.open(d, this);
								k.infobox.isOpen = true;
								d.panTo(k.getPosition())
							} else {
								k.infobox.close();
								k.infobox.isOpen = false
							}
						})
					})
}


$.fn.tabs = function() {
	var selector = this;
	
	this.each(function() {
		var obj = $(this); 
		
		$(obj.attr('href')).hide();
		
		$(obj).click(function() {
			$(selector).removeClass('selected');
			
			$(selector).each(function(i, element) {
				$($(element).attr('href')).hide();
			});
			
			$(this).addClass('selected');
			
			$($(this).attr('href')).fadeIn();
			
			var itemVal = $($(this).attr('href')).text();
			
			//show google map in hidden tab using jquery
			LoadMapProject();
			
			if(itemVal == "Map") {
				
			} 
			
			//Indo Headquarters
			if(itemVal == "Indonesia Headquarters") {
				
			}
			
			//Bali
			if(itemVal == "Bali Office") {
				
			}

			
			if(itemVal == "Surabaya Office") {
				
			}
			
			return false;
		});
	});

	$(this).show();
	
	$(this).first().click();
};

function LoadMapProject() {
	var e = $("#latListing").val();
	var c = $("#lngListing").val();
	var areaListing = $('#areaListing').val();
	var titleListing = $('#titleListing').val();
	var thumbListing = $('#thumbListing').val();
	
	var locations = new Array([e,c]);
    var markers = new Array();
    var mapOptions = {
        center: new google.maps.LatLng(e, c),
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false
    };

    var map = new google.maps.Map(document.getElementById('map_project_directory'), mapOptions);

    $.each(locations, function(index, location) {
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(location[0], location[1]),
            map: map,
            icon: 'http://html.realia.byaviators.com/assets/img/marker-transparent.png'
        });

        var myOptions = {
            content : '<div class="infobox"><div class="image"><img src="'+ thumbListing +'" alt=""></div><div class="title"><a href="detail.html">'+ titleListing +'</a></div><div class="area"><span class="key">Area:</span><span class="value">'+ areaListing +'</span></div></div>',
            disableAutoPan: false,
            maxWidth: 0,
            pixelOffset: new google.maps.Size(-146, -190),
            zIndex: null,
            closeBoxURL: "",
            infoBoxClearance: new google.maps.Size(1, 1),
            position: new google.maps.LatLng(location[0], location[1]),
            isHidden: false,
            pane: "floatPane",
            enableEventPropagation: false
        };
        marker.infobox = new InfoBox(myOptions);
        marker.infobox.isOpen = false;

        var myOptions = {
            draggable: true,
            content: '<div class="marker"><div class="marker-inner"></div></div>',
            disableAutoPan: true,
            pixelOffset: new google.maps.Size(-21, -58),
            position: new google.maps.LatLng(location[0], location[1]),
            closeBoxURL: "",
            isHidden: false,
            // pane: "mapPane",
            enableEventPropagation: true
        };
        marker.marker = new InfoBox(myOptions);
        marker.marker.open(map, marker);
        markers.push(marker);

        google.maps.event.addListener(marker, "click", function (e) {
            var curMarker = this;

            $.each(markers, function (index, marker) {
                // if marker is not the clicked marker, close the marker
                if (marker !== curMarker) {
                    marker.infobox.close();
                    marker.infobox.isOpen = false;
                }
            });


            if(curMarker.infobox.isOpen === false) {
                curMarker.infobox.open(map, this);
                curMarker.infobox.isOpen = true;
                map.panTo(curMarker.getPosition());
            } else {
                curMarker.infobox.close();
                curMarker.infobox.isOpen = false;
            }

        });
    });
}

function LoadMapContactUs() {
	var e = $("#latListing").val();
	var c = $("#lngListing").val();
	
	var locations = new Array([e,c]);
    var markers = new Array();
    var mapOptions = {
        center: new google.maps.LatLng(e, c),
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false
    };

    var map = new google.maps.Map(document.getElementById('map_contact_us'), mapOptions);

    $.each(locations, function(index, location) {
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(location[0], location[1]),
            map: map,
            icon: 'http://html.realia.byaviators.com/assets/img/marker-transparent.png'
        });

        var myOptions = {
            content : '',
            disableAutoPan: false,
            maxWidth: 0,
            pixelOffset: new google.maps.Size(-146, -190),
            zIndex: null,
            closeBoxURL: "",
            infoBoxClearance: new google.maps.Size(1, 1),
            position: new google.maps.LatLng(location[0], location[1]),
            isHidden: false,
            pane: "floatPane",
            enableEventPropagation: false
        };
        marker.infobox = new InfoBox(myOptions);
        marker.infobox.isOpen = false;

        var myOptions = {
            draggable: true,
            content: '<div class="marker"><div class="marker-inner"></div></div>',
            disableAutoPan: true,
            pixelOffset: new google.maps.Size(-21, -58),
            position: new google.maps.LatLng(location[0], location[1]),
            closeBoxURL: "",
            isHidden: false,
            // pane: "mapPane",
            enableEventPropagation: true
        };
        marker.marker = new InfoBox(myOptions);
        marker.marker.open(map, marker);
        markers.push(marker);

        google.maps.event.addListener(marker, "click", function (e) {
            var curMarker = this;

            $.each(markers, function (index, marker) {
                // if marker is not the clicked marker, close the marker
                if (marker !== curMarker) {
                    marker.infobox.close();
                    marker.infobox.isOpen = false;
                }
            });


            if(curMarker.infobox.isOpen === false) {
                curMarker.infobox.open(map, this);
                curMarker.infobox.isOpen = true;
                map.panTo(curMarker.getPosition());
            } else {
                curMarker.infobox.close();
                curMarker.infobox.isOpen = false;
            }

        });
    });
}

function InitMap() {
	google.maps.event.addDomListener(window, "load", LoadMapProperty);
	google.maps.event.addDomListener(window, "load", LoadMapProject);
	google.maps.event.addDomListener(window, "load", LoadMapContactUs);
};